package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.redis.RedisCache;
import lombok.RequiredArgsConstructor;

import javax.validation.constraints.*;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.vo.CaAppUserInfoVo;
import com.ruoyi.system.bo.CaAppUserInfoQueryBo;
import com.ruoyi.system.bo.CaAppUserInfoAddBo;
import com.ruoyi.system.bo.CaAppUserInfoEditBo;
import com.ruoyi.system.service.ICaAppUserInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 小程序用户Controller
 *
 * @author ruoyi
 * @date 2021-07-27
 */
@Validated
@Api(value = "小程序用户控制器", tags = {"小程序用户管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/userinfo")
public class CaAppUserInfoController extends BaseController {

    private final ICaAppUserInfoService iCaAppUserInfoService;

	@Autowired
	private RedisCache redisCache;

    /**
     * 查询小程序用户列表
     */
    @ApiOperation("查询小程序用户列表")
    //@PreAuthorize("@ss.hasPermi('system:userinfo:list')")
    @GetMapping("/list")
    public TableDataInfo<CaAppUserInfoVo> list(@Validated CaAppUserInfoQueryBo bo) {
        return iCaAppUserInfoService.queryPageList(bo);
    }

    /**
     * 导出小程序用户列表
     */
    @ApiOperation("导出小程序用户列表")
    @PreAuthorize("@ss.hasPermi('system:userinfo:export')")
    @Log(title = "小程序用户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<CaAppUserInfoVo> export(@Validated CaAppUserInfoQueryBo bo) {
        List<CaAppUserInfoVo> list = iCaAppUserInfoService.queryList(bo);
        ExcelUtil<CaAppUserInfoVo> util = new ExcelUtil<CaAppUserInfoVo>(CaAppUserInfoVo.class);
        return util.exportExcel(list, "小程序用户");
    }

    /**
     * 获取小程序用户详细信息
     */
    @ApiOperation("获取小程序用户详细信息")
    @PreAuthorize("@ss.hasPermi('system:userinfo:query')")
    @GetMapping("/{userId}")
    public AjaxResult<CaAppUserInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("userId") Long userId) {
        return AjaxResult.success(iCaAppUserInfoService.queryById(userId));
    }

    /**
     * 新增小程序用户
     */
    @ApiOperation("新增小程序用户")
    //@PreAuthorize("@ss.hasPermi('system:userinfo:add')")
    @Log(title = "小程序用户", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping("/inserCaAppUser")
    public AjaxResult<Void> inserCaAppUser(@Validated @RequestBody CaAppUserInfoAddBo bo,@RequestParam("code")String code) {
		String phone = bo.getPhone();
		String sessionCode = (String)redisCache.getCacheObject(phone);
		if(sessionCode.equals(code)){
			return toAjax(iCaAppUserInfoService.insertByAddBo(bo) ? 1 : 0);
		}else{
			return toAjax(false).setMsg("验证码错误");
		}

    }



	/**
	 * 新增小程序用户
	 * @return
	 */
	@ApiOperation("忘记密码")
	//@PreAuthorize("@ss.hasPermi('system:userinfo:add')")
	@Log(title = "忘记密码", businessType = BusinessType.INSERT)
	@RepeatSubmit
	@PostMapping("/forgetpassword")
	public AjaxResult<Void> forgetpassword(@RequestParam("phone")String phone, @RequestParam("code")String code, @RequestParam("password")String password) {
		String sessionCode = (String)redisCache.getCacheObject(phone);
		if(sessionCode.equals(code)){
			CaAppUserInfoQueryBo bo = new CaAppUserInfoQueryBo();
			bo.setPhone(phone);
			List<CaAppUserInfoVo> caAppUserInfoVos = iCaAppUserInfoService.queryList(bo);
			CaAppUserInfoVo caAppUserInfoVo = caAppUserInfoVos.get(0);
			CaAppUserInfoEditBo bo1 =new CaAppUserInfoEditBo();
			bo1.setAccount(caAppUserInfoVo.getAccount());
			bo1.setBirthday(caAppUserInfoVo.getBirthday());
			bo1.setEmail(caAppUserInfoVo.getEmail());
			bo1.setPassword(password);
			bo1.setPhone(caAppUserInfoVo.getPhone());
			bo1.setIsmanager(caAppUserInfoVo.getIsmanager());
			bo1.setLastIp(caAppUserInfoVo.getLastIp());
			bo1.setLastLocation(caAppUserInfoVo.getLastLocation());
			bo1.setLastTime(caAppUserInfoVo.getLastTime());
			bo1.setLocked(caAppUserInfoVo.getLocked());
			bo1.setPicture(caAppUserInfoVo.getPicture());
			bo1.setRoleId(caAppUserInfoVo.getRoleId());
			bo1.setScore(caAppUserInfoVo.getScore());
			bo1.setSex(caAppUserInfoVo.getSex());
			bo1.setUserId(caAppUserInfoVo.getUserId());
			bo1.setUserName(caAppUserInfoVo.getUserName());
			return toAjax(iCaAppUserInfoService.updateByEditBo(bo1) ? 1 : 0);
		}else{
			return toAjax(false).setMsg("验证码错误");
		}

	}




	/**
	 * 新增小程序用户
	 */
	@ApiOperation("新增小程序用户")
	//@PreAuthorize("@ss.hasPermi('system:userinfo:add')")
	@Log(title = "小程序用户", businessType = BusinessType.INSERT)
	@RepeatSubmit
	@PostMapping()
	public AjaxResult<Void> add(@Validated @RequestBody CaAppUserInfoAddBo bo) {
		return toAjax(iCaAppUserInfoService.insertByAddBo(bo) ? 1 : 0);
	}
    /**
     * 修改小程序用户
     */
    @ApiOperation("修改小程序用户")
    //@PreAuthorize("@ss.hasPermi('system:userinfo:edit')")
    @Log(title = "小程序用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody CaAppUserInfoEditBo bo) {
        return toAjax(iCaAppUserInfoService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除小程序用户
     */
    @ApiOperation("删除小程序用户")
    @PreAuthorize("@ss.hasPermi('system:userinfo:remove')")
    @Log(title = "小程序用户" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] userIds) {
        return toAjax(iCaAppUserInfoService.deleteWithValidByIds(Arrays.asList(userIds), true) ? 1 : 0);
    }
}
